import { config } from '../../config/index';
import { request } from '../_utils/request';

/** 获取商品列表 */
function mockFetchGood(ID = 0) {
  const { delay } = require('../_utils/delay');
  const { genGood } = require('../../model/good');
  return delay().then(() => genGood(ID));
}

// /** 获取商品列表 */
// export function fetchGood(ID = 0) {
//   if (config.useMock) {
//     return mockFetchGood(0);
//   }
//   return new Promise((resolve) => {
//     resolve('real api');
//   });
// }


//处理数据  
const processGoodData = (response) => {
  if (!response?.data) return null;
  
  const good = response.data;

  // 转换SKU列表
  const skuList = good.skus?.map(sku => ({
    skuId: sku.skuId,
    specInfo: sku.specValues.split(';').map(spec => {
      const [specName, valueText] = spec.split(':');
      const specItem = good.specs.find(s => s.specName === specName);
      const valueItem = good.specValues.find(
        v => v.specId === specItem.specId && v.valueName === valueText
      );
      
      return {
        specId: specItem.specId,
        specValueId: valueItem.valueId
      };
    }),
    price: sku.price * 100,
    originPrice: sku.linePrice * 100,
    quantity: sku.stock,
    image: sku.image || good.primaryImage
  })) || [];

  // 处理规格列表
  const specList = good.specs?.map(spec => ({
    specId: spec.specId,
    title: spec.specName,
    specValueList: good.specValues
      .filter(value => value.specId === spec.specId)
      .map(value => ({
        specValueId: value.valueId,
        specValue: value.valueName,
        specId: spec.specId,
        isSelected: false,
        hasStockObj: {
          hasStock: true,
          specsArray: []
        }
      }))
  })) || [];

  const result = {
    saasId: good.saasId?.toString() || '',
    storeId: good.storeId?.toString() || '',
    spuId: good.spuId?.toString() || '',
    title: good.title || '',
    primaryImage: good.primaryImage || '',
    images: JSON.parse(good.images || '[]'),
    video: good.video || null,
    available: good.status === 1 ? 1 : 0,
    minSalePrice: good.minSalePrice * 100,
    minLinePrice: good.minLinePrice * 100,
    maxSalePrice: good.maxSalePrice * 100,
    maxLinePrice: good.maxLinePrice * 100,
    spuStockQuantity: good.spuStockQuantity || 0,
    soldNum: good.soldQuantity || 0,
    isPutOnSale: good.status === 1 ? 1 : 0,
    categoryIds: [good.cateId?.toString() || ''],
    specList,
    skuList,
    spuTagList: good.spuTagList ? good.spuTagList.map(tag => ({
      id: tag.id?.toString(),
      title: tag.title,
      image: tag.image || null
    })) : [],
    limitInfo: [{
      text: '限购5件'
    }],
    desc: JSON.parse(good.desc || '[]'),
    etitle: good.brief || ''
  };

  console.log('处理后的完整数据:', result);
  return result;
};


/** 获取商品列表 */
export function fetchGood(spuId) {
  // if (config.useMock) {
  //   return mockFetchGood(spuId);
  // }
  
  const mygood=request({  
    url: `/Good/detail/${spuId}`,
    method: 'get',
   
  }).then(response => processGoodData(response));
  console.log(mygood);

  return mygood

}



